package dbutils.dao.impl;

import dbutils.dao.HandlerDao;
import dbutils.entity.Staff;
import dbutils.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * @author 20x
 * @date 2025 - 02 -27
 **/
public class HandlerDaoImpl implements HandlerDao {
    QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

    @Override
    public Map<String, Object> selectByIdReturnMap(int id) {
        String sql = "select id, name, age, phone, sta_pos as staPos, add_time as addTime, update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql, new MapHandler(), id);
        } catch (SQLException e) {
            throw new RuntimeException("Failed to fetch staff by id: " + id, e);
        }
    }

    @Override
    public List<Map<String, Object>> selectAll() {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql,new MapListHandler());
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Staff selectById(int id) {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql, new BeanHandler<>(Staff.class), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Staff> getStaffAll() {
        String sql = "select id, name, age, phone, sta_pos as staPos, add_time as addTime, update_time as updateTime from staffs";
        try {
            return qr.query(sql, new BeanListHandler<>(Staff.class));
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectNameById(int id) {
        String sql = "select name from staffs where id = ?";
         try {
                return qr.query(sql, new ScalarHandler<>(), id);
         }
         catch (SQLException e) {
             throw new RuntimeException(e);
         }
    }

    @Override
    public long count() {
        String sql = "select count(*) from staffs";
        try {
            return qr.query(sql, new ScalarHandler<Long>());
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> selectNameAll() {
        String sql = "select name from staffs";
        try {
            return qr.query(sql, new ColumnListHandler<>());
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object[] selectByIdReturnObject(int id) {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs where id=?";
        try {
            return qr.query(sql, new ArrayHandler(), id);
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Object[]> selectAllReturnObject() {
        String sql = "select id,name,age,phone,sta_pos as staPos,add_time as addTime,update_time as updateTime from staffs";
        try {
            return qr.query(sql, new ArrayListHandler());
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        HandlerDao handlerDao = new HandlerDaoImpl();
//        System.out.println(handlerDao.selectByIdReturnMap(1));
//        System.out.println(handlerDao.selectAll());
//        System.out.println(handlerDao.selectById(1));
//        System.out.println(handlerDao.getStaffAll());
//        System.out.println(handlerDao.selectNameById(1));
//        System.out.println(handlerDao.count());
//        System.out.println(handlerDao.selectNameAll());
        System.out.println(Arrays.toString(handlerDao.selectByIdReturnObject(1)));


    }
}
